package testcase.iotest;

import junit.framework.TestCase;
import io.*;
import card.Card;
import card.Cardset;

public class IOManagerIntegrationTest extends TestCase {

	class InputList {
		private String[] input;
		private int current;
		
		public InputList(String[] input) {
			current = 0;
			this.input = input;
		}
		
		public String getNext(){
			return input[current++];			
		}
		
	}
	
	class IOStub implements IIO {
		public String lastPrint;
		private InputList inputList;
		
		public IOStub(String[] input)
		{
			inputList = new InputList(input);
		}

		@Override
		public String getNextInput() {
			return inputList.getNext();
		}

		@Override
		public void println() {
		}

		@Override
		public void println(String s) {
			lastPrint = s;
		}

		@Override
		public void print(String s) {
			lastPrint = s;
		}

		@Override
		public void print(Card c, int index) {
		}

		@Override
		public void print(Cardset cs) {
		}
	}

	public void testPlayerSelectCards() {
		String input[] = new String[2];
		input[0] = "1";
		input[1] = "t";
		
		IOManager ioManager = new IOManager(new IOStub(input));

		Cardset cards = new Cardset(3);
		Card card1 = new Card('d', '3');
		Card card2 = new Card('d', '4');
		cards.add(card1);
		cards.add(card2);

		Cardset result = ioManager.ioPlayerSelectCards(cards, "test");
		assertEquals(result.count(), 1);
		assertEquals(true, result.getCard(0).compare(card2));
	}

	public void testEndGameMessage() {
		IOStub io = new IOStub(null);
		IOManager ioManager = new IOManager(io);
		ioManager.ioEndGameMessage();

		assertEquals("Game End!", io.lastPrint);
	}

	public void testNewRoundMessage() {
		IOStub io = new IOStub(null);
		IOManager ioManager = new IOManager(io);
		ioManager.ioNewRoundMessage("A");
		
		assertEquals("You can discard any card as you want.", io.lastPrint);
	}

	public void testNotBiggerPlayMessage() {
		IOStub io = new IOStub(null);
		IOManager ioManager = new IOManager(io);
		ioManager.ioNotBiggerPlayMessage();

		assertEquals("------------------------------------------------", io.lastPrint);
	}

	public void testConfirmPlay() {
		String input[] = new String[1];
		input[0] = "c";
		
		IOStub io = new IOStub(input);
		IOManager ioManager = new IOManager(io);
		char result = ioManager.ioConfirmPlay(new Cardset(3));

		assertEquals(result, 'c');
	}

	public void testContinueGame() {
		String input[] = new String[1];
		input[0] = "y";
		
		IOStub io = new IOStub(input);
		IOManager ioManager = new IOManager(io);
		char result = ioManager.ioContinueGame("test");

		assertEquals(result, 'y');
	}

	public void testEndApplicationMessage() {
		IOStub io = new IOStub(null);
		IOManager ioManager = new IOManager(io);
		
		ioManager.ioEndApplicationMessage();

		assertEquals("== End ==", io.lastPrint);
	}

	public void testPlayerLandlordRating() {
		String input[] = new String[1];
		input[0] = "2";
		
		IOStub io = new IOStub(input);
		IOManager ioManager = new IOManager(io);
		int result = ioManager.ioPlayerLandlordRating("test", new Cardset(3));

		assertEquals(2, result);
	}

	public void testIOManagerCreation() {
		boolean succeed = true;

		try {
			IOManager ioManager = new IOManager();
			ioManager.ioEndApplicationMessage();
		} catch (Exception c) {
			succeed = false;
		}

		assertEquals(true, succeed);

	}

}
